home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / tcltk / tk8.5 / clrpick.tcl < prev    next >
Encoding:
Text File  |  2009-11-17  |  20.8 KB  |  695 lines

  1. # clrpick.tcl --
  2. #
  3. #    Color selection dialog for platforms that do not support a
  4. #    standard color selection dialog.
  5. #
  6. # RCS: @(#) $Id: clrpick.tcl,v 1.22 2006/03/17 11:13:15 patthoyts Exp $
  7. #
  8. # Copyright (c) 1996 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # ToDo:
  14. #
  15. #    (1): Find out how many free colors are left in the colormap and
  16. #         don't allocate too many colors.
  17. #    (2): Implement HSV color selection. 
  18. #
  19.  
  20. # Make sure namespaces exist
  21. namespace eval ::tk {}
  22. namespace eval ::tk::dialog {}
  23. namespace eval ::tk::dialog::color {
  24.     namespace import ::tk::msgcat::*
  25. }
  26.  
  27. # ::tk::dialog::color:: --
  28. #
  29. #    Create a color dialog and let the user choose a color. This function
  30. #    should not be called directly. It is called by the tk_chooseColor
  31. #    function when a native color selector widget does not exist
  32. #
  33. proc ::tk::dialog::color:: {args} {
  34.     variable ::tk::Priv
  35.     set dataName __tk__color
  36.     upvar ::tk::dialog::color::$dataName data
  37.     set w .$dataName
  38.  
  39.     # The lines variables track the start and end indices of the line
  40.     # elements in the colorbar canvases.
  41.     set data(lines,red,start)   0
  42.     set data(lines,red,last)   -1
  43.     set data(lines,green,start) 0
  44.     set data(lines,green,last) -1
  45.     set data(lines,blue,start)  0
  46.     set data(lines,blue,last)  -1
  47.  
  48.     # This is the actual number of lines that are drawn in each color strip.
  49.     # Note that the bars may be of any width.
  50.     # However, NUM_COLORBARS must be a number that evenly divides 256.
  51.     # Such as 256, 128, 64, etc.
  52.     set data(NUM_COLORBARS) 16
  53.  
  54.     # BARS_WIDTH is the number of pixels wide the color bar portion of the
  55.     # canvas is. This number must be a multiple of NUM_COLORBARS
  56.     set data(BARS_WIDTH) 160
  57.  
  58.     # PLGN_WIDTH is the number of pixels wide of the triangular selection
  59.     # polygon. This also results in the definition of the padding on the 
  60.     # left and right sides which is half of PLGN_WIDTH. Make this number even.
  61.     set data(PLGN_HEIGHT) 10
  62.  
  63.     # PLGN_HEIGHT is the height of the selection polygon and the height of the 
  64.     # selection rectangle at the bottom of the color bar. No restrictions.
  65.     set data(PLGN_WIDTH) 10
  66.  
  67.     Config $dataName $args
  68.     InitValues $dataName
  69.  
  70.     set sc [winfo screen $data(-parent)]
  71.     set winExists [winfo exists $w]
  72.     if {!$winExists || $sc ne [winfo screen $w]} {
  73.     if {$winExists} {
  74.         destroy $w
  75.     }
  76.     toplevel $w -class TkColorDialog -screen $sc
  77.     BuildDialog $w
  78.     }
  79.  
  80.     # Dialog boxes should be transient with respect to their parent,
  81.     # so that they will always stay on top of their parent window.  However,
  82.     # some window managers will create the window as withdrawn if the parent
  83.     # window is withdrawn or iconified.  Combined with the grab we put on the
  84.     # window, this can hang the entire application.  Therefore we only make
  85.     # the dialog transient if the parent is viewable.
  86.  
  87.     if {[winfo viewable [winfo toplevel $data(-parent)]] } {
  88.     wm transient $w $data(-parent)
  89.     }
  90.  
  91.     # 5. Withdraw the window, then update all the geometry information
  92.     # so we know how big it wants to be, then center the window in the
  93.     # display and de-iconify it.
  94.  
  95.     ::tk::PlaceWindow $w widget $data(-parent)
  96.     wm title $w $data(-title)
  97.  
  98.     # 6. Set a grab and claim the focus too.
  99.  
  100.     ::tk::SetFocusGrab $w $data(okBtn)
  101.  
  102.     # 7. Wait for the user to respond, then restore the focus and
  103.     # return the index of the selected button.  Restore the focus
  104.     # before deleting the window, since otherwise the window manager
  105.     # may take the focus away so we can't redirect it.  Finally,
  106.     # restore any grab that was in effect.
  107.  
  108.     vwait ::tk::Priv(selectColor)
  109.     set result $Priv(selectColor)
  110.     ::tk::RestoreFocusGrab $w $data(okBtn)
  111.     unset data
  112.  
  113.     return $result
  114. }
  115.  
  116. # ::tk::dialog::color::InitValues --
  117. #
  118. #    Get called during initialization or when user resets NUM_COLORBARS
  119. #
  120. proc ::tk::dialog::color::InitValues {dataName} {
  121.     upvar ::tk::dialog::color::$dataName data
  122.  
  123.     # IntensityIncr is the difference in color intensity between a colorbar
  124.     # and its neighbors.
  125.     set data(intensityIncr) [expr {256 / $data(NUM_COLORBARS)}]
  126.  
  127.     # ColorbarWidth is the width of each colorbar
  128.     set data(colorbarWidth) [expr {$data(BARS_WIDTH) / $data(NUM_COLORBARS)}]
  129.  
  130.     # Indent is the width of the space at the left and right side of the
  131.     # colorbar. It is always half the selector polygon width, because the
  132.     # polygon extends into the space.
  133.     set data(indent) [expr {$data(PLGN_WIDTH) / 2}]
  134.  
  135.     set data(colorPad) 2
  136.     set data(selPad)   [expr {$data(PLGN_WIDTH) / 2}]
  137.  
  138.     #
  139.     # minX is the x coordinate of the first colorbar
  140.     #
  141.     set data(minX) $data(indent)
  142.  
  143.     #
  144.     # maxX is the x coordinate of the last colorbar
  145.     #
  146.     set data(maxX) [expr {$data(BARS_WIDTH) + $data(indent)-1}]
  147.  
  148.     #
  149.     # canvasWidth is the width of the entire canvas, including the indents
  150.     #
  151.     set data(canvasWidth) [expr {$data(BARS_WIDTH) + $data(PLGN_WIDTH)}]
  152.  
  153.     # Set the initial color, specified by -initialcolor, or the
  154.     # color chosen by the user the last time.
  155.     set data(selection) $data(-initialcolor)
  156.     set data(finalColor)  $data(-initialcolor)
  157.     set rgb [winfo rgb . $data(selection)]
  158.  
  159.     set data(red,intensity)   [expr {[lindex $rgb 0]/0x100}]
  160.     set data(green,intensity) [expr {[lindex $rgb 1]/0x100}]
  161.     set data(blue,intensity)  [expr {[lindex $rgb 2]/0x100}]
  162. }
  163.  
  164. # ::tk::dialog::color::Config  --
  165. #
  166. #    Parses the command line arguments to tk_chooseColor
  167. #
  168. proc ::tk::dialog::color::Config {dataName argList} {
  169.     variable ::tk::Priv
  170.     upvar ::tk::dialog::color::$dataName data
  171.  
  172.     # 1: the configuration specs
  173.     #
  174.     if {[info exists Priv(selectColor)] && $Priv(selectColor) ne ""} {
  175.     set defaultColor $Priv(selectColor)
  176.     } else {
  177.     set defaultColor [. cget -background]
  178.     }
  179.  
  180.     set specs [list \
  181.         [list -initialcolor "" "" $defaultColor] \
  182.         [list -parent "" "" "."] \
  183.         [list -title "" "" [mc "Color"]] \
  184.         ]
  185.  
  186.     # 2: parse the arguments
  187.     #
  188.     tclParseConfigSpec ::tk::dialog::color::$dataName $specs "" $argList
  189.  
  190.     if {$data(-title) eq ""} {
  191.     set data(-title) " "
  192.     }
  193.     if {[catch {winfo rgb . $data(-initialcolor)} err]} {
  194.     error $err
  195.     }
  196.  
  197.     if {![winfo exists $data(-parent)]} {
  198.     error "bad window path name \"$data(-parent)\""
  199.     }
  200. }
  201.  
  202. # ::tk::dialog::color::BuildDialog --
  203. #
  204. #    Build the dialog.
  205. #
  206. proc ::tk::dialog::color::BuildDialog {w} {
  207.     upvar ::tk::dialog::color::[winfo name $w] data
  208.  
  209.     # TopFrame contains the color strips and the color selection
  210.     #
  211.     set topFrame [frame $w.top -relief raised -bd 1]
  212.  
  213.     # StripsFrame contains the colorstrips and the individual RGB entries
  214.     set stripsFrame [frame $topFrame.colorStrip]
  215.  
  216.     set maxWidth [::tk::mcmaxamp &Red &Green &Blue]
  217.     set maxWidth [expr {$maxWidth<6 ? 6 : $maxWidth}]
  218.     set colorList {
  219.     red   "&Red"
  220.     green "&Green"
  221.     blue  "&Blue"
  222.     }
  223.     foreach {color l} $colorList {
  224.     # each f frame contains an [R|G|B] entry and the equiv. color strip.
  225.     set f [frame $stripsFrame.$color]
  226.  
  227.     # The box frame contains the label and entry widget for an [R|G|B]
  228.     set box [frame $f.box]
  229.  
  230.     ::tk::AmpWidget label $box.label -text "[mc $l]:" \
  231.         -width $maxWidth -anchor ne
  232.     bind $box.label <<AltUnderlined>> [list focus $box.entry]
  233.  
  234.     entry $box.entry -textvariable \
  235.         ::tk::dialog::color::[winfo name $w]($color,intensity) \
  236.         -width 4
  237.     pack $box.label -side left -fill y -padx 2 -pady 3
  238.     pack $box.entry -side left -anchor n -pady 0
  239.     pack $box -side left -fill both
  240.  
  241.     set height [expr {
  242.         [winfo reqheight $box.entry] -
  243.         2*([$box.entry cget -highlightthickness] + [$box.entry cget -bd])
  244.     }]
  245.  
  246.     canvas $f.color -height $height \
  247.         -width $data(BARS_WIDTH) -relief sunken -bd 2
  248.     canvas $f.sel -height $data(PLGN_HEIGHT) \
  249.         -width $data(canvasWidth) -highlightthickness 0
  250.     pack $f.color -expand yes -fill both
  251.     pack $f.sel -expand yes -fill both
  252.  
  253.     pack $f -side top -fill x -padx 0 -pady 2
  254.  
  255.     set data($color,entry) $box.entry
  256.     set data($color,col) $f.color
  257.     set data($color,sel) $f.sel
  258.  
  259.     bind $data($color,col) <Configure> \
  260.         [list tk::dialog::color::DrawColorScale $w $color 1]
  261.     bind $data($color,col) <Enter> \
  262.         [list tk::dialog::color::EnterColorBar $w $color]
  263.     bind $data($color,col) <Leave> \
  264.         [list tk::dialog::color::LeaveColorBar $w $color]
  265.  
  266.     bind $data($color,sel) <Enter> \
  267.         [list tk::dialog::color::EnterColorBar $w $color]
  268.     bind $data($color,sel) <Leave> \
  269.         [list tk::dialog::color::LeaveColorBar $w $color]
  270.  
  271.     bind $box.entry <Return> [list tk::dialog::color::HandleRGBEntry $w]
  272.     }
  273.  
  274.     pack $stripsFrame -side left -fill both -padx 4 -pady 10
  275.  
  276.     # The selFrame contains a frame that demonstrates the currently
  277.     # selected color
  278.     #
  279.     set selFrame [frame $topFrame.sel]
  280.     set lab [::tk::AmpWidget label $selFrame.lab \
  281.         -text [mc "&Selection:"] -anchor sw]
  282.     set ent [entry $selFrame.ent \
  283.         -textvariable ::tk::dialog::color::[winfo name $w](selection) \
  284.         -width 16]
  285.     set f1  [frame $selFrame.f1 -relief sunken -bd 2]
  286.     set data(finalCanvas) [frame $f1.demo -bd 0 -width 100 -height 70]
  287.  
  288.     pack $lab $ent -side top -fill x -padx 4 -pady 2
  289.     pack $f1 -expand yes -anchor nw -fill both -padx 6 -pady 10
  290.     pack $data(finalCanvas) -expand yes -fill both
  291.  
  292.     bind $ent <Return> [list tk::dialog::color::HandleSelEntry $w]
  293.  
  294.     pack $selFrame -side left -fill none -anchor nw
  295.     pack $topFrame -side top -expand yes -fill both -anchor nw
  296.  
  297.     # the botFrame frame contains the buttons
  298.     #
  299.     set botFrame [frame $w.bot -relief raised -bd 1]
  300.  
  301.     ::tk::AmpWidget button $botFrame.ok     -text [mc "&OK"]        \
  302.         -command [list tk::dialog::color::OkCmd $w]
  303.     ::tk::AmpWidget button $botFrame.cancel -text [mc "&Cancel"]    \
  304.         -command [list tk::dialog::color::CancelCmd $w]
  305.  
  306.     set data(okBtn)      $botFrame.ok
  307.     set data(cancelBtn)  $botFrame.cancel
  308.  
  309.     grid x $botFrame.ok x $botFrame.cancel x -sticky ew
  310.     grid configure $botFrame.ok $botFrame.cancel -padx 10 -pady 10
  311.     grid columnconfigure $botFrame {0 4} -weight 1 -uniform space
  312.     grid columnconfigure $botFrame {1 3} -weight 1 -uniform button
  313.     grid columnconfigure $botFrame 2 -weight 2 -uniform space
  314.     pack $botFrame -side bottom -fill x
  315.  
  316.     # Accelerator bindings
  317.     bind $lab <<AltUnderlined>> [list focus $ent]
  318.     bind $w <KeyPress-Escape> [list tk::ButtonInvoke $data(cancelBtn)]
  319.     bind $w <Alt-Key> [list tk::AltKeyInDialog $w %A]
  320.  
  321.     wm protocol $w WM_DELETE_WINDOW [list tk::dialog::color::CancelCmd $w]
  322.     bind $lab <Destroy> [list tk::dialog::color::CancelCmd $w]
  323. }
  324.  
  325. # ::tk::dialog::color::SetRGBValue --
  326. #
  327. #    Sets the current selection of the dialog box
  328. #
  329. proc ::tk::dialog::color::SetRGBValue {w color} {
  330.     upvar ::tk::dialog::color::[winfo name $w] data 
  331.  
  332.     set data(red,intensity)   [lindex $color 0]
  333.     set data(green,intensity) [lindex $color 1]
  334.     set data(blue,intensity)  [lindex $color 2]
  335.  
  336.     RedrawColorBars $w all
  337.  
  338.     # Now compute the new x value of each colorbars pointer polygon
  339.     foreach color {red green blue} {
  340.     set x [RgbToX $w $data($color,intensity)]
  341.     MoveSelector $w $data($color,sel) $color $x 0
  342.     }
  343. }
  344.  
  345. # ::tk::dialog::color::XToRgb --
  346. #
  347. #    Converts a screen coordinate to intensity
  348. #
  349. proc ::tk::dialog::color::XToRgb {w x} {
  350.     upvar ::tk::dialog::color::[winfo name $w] data
  351.  
  352.     set x [expr {($x * $data(intensityIncr))/ $data(colorbarWidth)}]
  353.     if {$x > 255} {
  354.     set x 255
  355.     }
  356.     return $x
  357. }
  358.  
  359. # ::tk::dialog::color::RgbToX
  360. #
  361. #    Converts an intensity to screen coordinate.
  362. #
  363. proc ::tk::dialog::color::RgbToX {w color} {
  364.     upvar ::tk::dialog::color::[winfo name $w] data
  365.  
  366.     return [expr {($color * $data(colorbarWidth)/ $data(intensityIncr))}]
  367. }
  368.  
  369. # ::tk::dialog::color::DrawColorScale --
  370. #    Draw color scale is called whenever the size of one of the color
  371. #    scale canvases is changed.
  372. #
  373. proc ::tk::dialog::color::DrawColorScale {w c {create 0}} {
  374.     upvar ::tk::dialog::color::[winfo name $w] data
  375.  
  376.     # col: color bar canvas
  377.     # sel: selector canvas
  378.     set col $data($c,col)
  379.     set sel $data($c,sel)
  380.  
  381.     # First handle the case that we are creating everything for the first time.
  382.     if {$create} {
  383.     # First remove all the lines that already exist.
  384.     if { $data(lines,$c,last) > $data(lines,$c,start)} {
  385.         for {set i $data(lines,$c,start)} \
  386.             {$i <= $data(lines,$c,last)} {incr i} {
  387.         $sel delete $i
  388.         }
  389.     }
  390.     # Delete the selector if it exists
  391.     if {[info exists data($c,index)]} {
  392.         $sel delete $data($c,index)
  393.     }
  394.  
  395.     # Draw the selection polygons
  396.     CreateSelector $w $sel $c
  397.     $sel bind $data($c,index) <ButtonPress-1> \
  398.         [list tk::dialog::color::StartMove $w $sel $c %x $data(selPad) 1]
  399.     $sel bind $data($c,index) <B1-Motion> \
  400.         [list tk::dialog::color::MoveSelector $w $sel $c %x $data(selPad)]
  401.     $sel bind $data($c,index) <ButtonRelease-1> \
  402.         [list tk::dialog::color::ReleaseMouse $w $sel $c %x $data(selPad)]
  403.  
  404.     set height [winfo height $col]
  405.     # Create an invisible region under the colorstrip to catch mouse clicks
  406.     # that aren't on the selector.
  407.     set data($c,clickRegion) [$sel create rectangle 0 0 \
  408.         $data(canvasWidth) $height -fill {} -outline {}]
  409.  
  410.     bind $col <ButtonPress-1> \
  411.         [list tk::dialog::color::StartMove $w $sel $c %x $data(colorPad)]
  412.     bind $col <B1-Motion> \
  413.         [list tk::dialog::color::MoveSelector $w $sel $c %x $data(colorPad)]
  414.     bind $col <ButtonRelease-1> \
  415.         [list tk::dialog::color::ReleaseMouse $w $sel $c %x $data(colorPad)]
  416.  
  417.     $sel bind $data($c,clickRegion) <ButtonPress-1> \
  418.         [list tk::dialog::color::StartMove $w $sel $c %x $data(selPad)]
  419.     $sel bind $data($c,clickRegion) <B1-Motion> \
  420.         [list tk::dialog::color::MoveSelector $w $sel $c %x $data(selPad)]
  421.     $sel bind $data($c,clickRegion) <ButtonRelease-1> \
  422.         [list tk::dialog::color::ReleaseMouse $w $sel $c %x $data(selPad)]
  423.     } else {
  424.     # l is the canvas index of the first colorbar.
  425.     set l $data(lines,$c,start)
  426.     }
  427.  
  428.     # Draw the color bars.
  429.     set highlightW [expr {[$col cget -highlightthickness] + [$col cget -bd]}]
  430.     for {set i 0} { $i < $data(NUM_COLORBARS)} { incr i} {
  431.     set intensity [expr {$i * $data(intensityIncr)}]
  432.     set startx [expr {$i * $data(colorbarWidth) + $highlightW}]
  433.     if {$c eq "red"} {
  434.         set color [format "#%02x%02x%02x" \
  435.             $intensity $data(green,intensity) $data(blue,intensity)]
  436.     } elseif {$c eq "green"} {
  437.         set color [format "#%02x%02x%02x" \
  438.             $data(red,intensity) $intensity $data(blue,intensity)]
  439.     } else {
  440.         set color [format "#%02x%02x%02x" \
  441.             $data(red,intensity) $data(green,intensity) $intensity]
  442.     }
  443.  
  444.     if {$create} {
  445.         set index [$col create rect $startx $highlightW \
  446.             [expr {$startx +$data(colorbarWidth)}] \
  447.             [expr {[winfo height $col] + $highlightW}] \
  448.             -fill $color -outline $color]
  449.     } else {
  450.         $col itemconfigure $l -fill $color -outline $color
  451.         incr l
  452.     }
  453.     }
  454.     $sel raise $data($c,index)
  455.  
  456.     if {$create} {
  457.     set data(lines,$c,last) $index
  458.     set data(lines,$c,start) [expr {$index - $data(NUM_COLORBARS) + 1}]
  459.     }
  460.  
  461.     RedrawFinalColor $w
  462. }
  463.  
  464. # ::tk::dialog::color::CreateSelector --
  465. #
  466. #    Creates and draws the selector polygon at the position
  467. #    $data($c,intensity).
  468. #
  469. proc ::tk::dialog::color::CreateSelector {w sel c } {
  470.     upvar ::tk::dialog::color::[winfo name $w] data
  471.     set data($c,index) [$sel create polygon \
  472.         0 $data(PLGN_HEIGHT) \
  473.         $data(PLGN_WIDTH) $data(PLGN_HEIGHT) \
  474.         $data(indent) 0]
  475.     set data($c,x) [RgbToX $w $data($c,intensity)]
  476.     $sel move $data($c,index) $data($c,x) 0
  477. }
  478.  
  479. # ::tk::dialog::color::RedrawFinalColor
  480. #
  481. #    Combines the intensities of the three colors into the final color
  482. #
  483. proc ::tk::dialog::color::RedrawFinalColor {w} {
  484.     upvar ::tk::dialog::color::[winfo name $w] data
  485.  
  486.     set color [format "#%02x%02x%02x" $data(red,intensity) \
  487.         $data(green,intensity) $data(blue,intensity)]
  488.  
  489.     $data(finalCanvas) configure -bg $color
  490.     set data(finalColor) $color
  491.     set data(selection) $color
  492.     set data(finalRGB) [list \
  493.         $data(red,intensity) \
  494.         $data(green,intensity) \
  495.         $data(blue,intensity)]
  496. }
  497.  
  498. # ::tk::dialog::color::RedrawColorBars --
  499. #
  500. # Only redraws the colors on the color strips that were not manipulated.
  501. # Params: color of colorstrip that changed. If color is not [red|green|blue]
  502. #         Then all colorstrips will be updated
  503. #
  504. proc ::tk::dialog::color::RedrawColorBars {w colorChanged} {
  505.     upvar ::tk::dialog::color::[winfo name $w] data
  506.  
  507.     switch $colorChanged {
  508.     red { 
  509.         DrawColorScale $w green
  510.         DrawColorScale $w blue
  511.     }
  512.     green {
  513.         DrawColorScale $w red
  514.         DrawColorScale $w blue
  515.     }
  516.     blue {
  517.         DrawColorScale $w red
  518.         DrawColorScale $w green
  519.     }
  520.     default {
  521.         DrawColorScale $w red
  522.         DrawColorScale $w green
  523.         DrawColorScale $w blue
  524.     }
  525.     }
  526.     RedrawFinalColor $w
  527. }
  528.  
  529. #----------------------------------------------------------------------
  530. #            Event handlers
  531. #----------------------------------------------------------------------
  532.  
  533. # ::tk::dialog::color::StartMove --
  534. #
  535. #    Handles a mousedown button event over the selector polygon.
  536. #    Adds the bindings for moving the mouse while the button is
  537. #    pressed.  Sets the binding for the button-release event.
  538. # Params: sel is the selector canvas window, color is the color of the strip.
  539. #
  540. proc ::tk::dialog::color::StartMove {w sel color x delta {dontMove 0}} {
  541.     upvar ::tk::dialog::color::[winfo name $w] data
  542.  
  543.     if {!$dontMove} {
  544.     MoveSelector $w $sel $color $x $delta
  545.     }
  546. }
  547.  
  548. # ::tk::dialog::color::MoveSelector --
  549. # Moves the polygon selector so that its middle point has the same
  550. # x value as the specified x. If x is outside the bounds [0,255],
  551. # the selector is set to the closest endpoint.
  552. #
  553. # Params: sel is the selector canvas, c is [red|green|blue]
  554. #         x is a x-coordinate.
  555. #
  556. proc ::tk::dialog::color::MoveSelector {w sel color x delta} {
  557.     upvar ::tk::dialog::color::[winfo name $w] data
  558.  
  559.     incr x -$delta
  560.  
  561.     if { $x < 0 } {
  562.     set x 0
  563.     } elseif { $x > $data(BARS_WIDTH)} {
  564.     set x $data(BARS_WIDTH)
  565.     }
  566.     set diff [expr {$x - $data($color,x)}]
  567.     $sel move $data($color,index) $diff 0
  568.     set data($color,x) [expr {$data($color,x) + $diff}]
  569.  
  570.     # Return the x value that it was actually set at
  571.     return $x
  572. }
  573.  
  574. # ::tk::dialog::color::ReleaseMouse
  575. #
  576. # Removes mouse tracking bindings, updates the colorbars.
  577. #
  578. # Params: sel is the selector canvas, color is the color of the strip,
  579. #         x is the x-coord of the mouse.
  580. #
  581. proc ::tk::dialog::color::ReleaseMouse {w sel color x delta} {
  582.     upvar ::tk::dialog::color::[winfo name $w] data 
  583.  
  584.     set x [MoveSelector $w $sel $color $x $delta]
  585.  
  586.     # Determine exactly what color we are looking at.
  587.     set data($color,intensity) [XToRgb $w $x]
  588.  
  589.     RedrawColorBars $w $color
  590. }
  591.  
  592. # ::tk::dialog::color::ResizeColorbars --
  593. #
  594. #    Completely redraws the colorbars, including resizing the
  595. #    colorstrips
  596. #
  597. proc ::tk::dialog::color::ResizeColorBars {w} {
  598.     upvar ::tk::dialog::color::[winfo name $w] data
  599.  
  600.     if {
  601.     ($data(BARS_WIDTH) < $data(NUM_COLORBARS)) || 
  602.     (($data(BARS_WIDTH) % $data(NUM_COLORBARS)) != 0)
  603.     } then {
  604.     set data(BARS_WIDTH) $data(NUM_COLORBARS)
  605.     }
  606.     InitValues [winfo name $w]
  607.     foreach color {red green blue} {
  608.     $data($color,col) configure -width $data(canvasWidth)
  609.     DrawColorScale $w $color 1
  610.     }
  611. }
  612.  
  613. # ::tk::dialog::color::HandleSelEntry --
  614. #
  615. #    Handles the return keypress event in the "Selection:" entry
  616. #
  617. proc ::tk::dialog::color::HandleSelEntry {w} {
  618.     upvar ::tk::dialog::color::[winfo name $w] data
  619.  
  620.     set text [string trim $data(selection)]
  621.     # Check to make sure that the color is valid
  622.     if {[catch {set color [winfo rgb . $text]} ]} {
  623.     set data(selection) $data(finalColor)
  624.     return
  625.     }
  626.  
  627.     set R [expr {[lindex $color 0]/0x100}]
  628.     set G [expr {[lindex $color 1]/0x100}]
  629.     set B [expr {[lindex $color 2]/0x100}]
  630.  
  631.     SetRGBValue $w "$R $G $B"
  632.     set data(selection) $text
  633. }
  634.  
  635. # ::tk::dialog::color::HandleRGBEntry --
  636. #
  637. #    Handles the return keypress event in the R, G or B entry
  638. #
  639. proc ::tk::dialog::color::HandleRGBEntry {w} {
  640.     upvar ::tk::dialog::color::[winfo name $w] data
  641.  
  642.     foreach c {red green blue} {
  643.     if {[catch {
  644.         set data($c,intensity) [expr {int($data($c,intensity))}]
  645.     }]} {
  646.         set data($c,intensity) 0
  647.     }
  648.  
  649.     if {$data($c,intensity) < 0} {
  650.         set data($c,intensity) 0
  651.     }
  652.     if {$data($c,intensity) > 255} {
  653.         set data($c,intensity) 255
  654.     }
  655.     }
  656.  
  657.     SetRGBValue $w "$data(red,intensity) \
  658.     $data(green,intensity) $data(blue,intensity)"
  659. }    
  660.  
  661. # mouse cursor enters a color bar
  662. #
  663. proc ::tk::dialog::color::EnterColorBar {w color} {
  664.     upvar ::tk::dialog::color::[winfo name $w] data
  665.  
  666.     $data($color,sel) itemconfigure $data($color,index) -fill red
  667. }
  668.  
  669. # mouse leaves enters a color bar
  670. #
  671. proc ::tk::dialog::color::LeaveColorBar {w color} {
  672.     upvar ::tk::dialog::color::[winfo name $w] data
  673.  
  674.     $data($color,sel) itemconfigure $data($color,index) -fill black
  675. }
  676.  
  677. # user hits OK button
  678. #
  679. proc ::tk::dialog::color::OkCmd {w} {
  680.     variable ::tk::Priv
  681.     upvar ::tk::dialog::color::[winfo name $w] data
  682.  
  683.     set Priv(selectColor) $data(finalColor)
  684. }
  685.  
  686. # user hits Cancel button or destroys window
  687. #
  688. proc ::tk::dialog::color::CancelCmd {w} {
  689.     variable ::tk::Priv
  690.     set Priv(selectColor) ""
  691. }
  692.